<template>
	<view class="default-background">
		<view class="publish-house-page">
			<view class="publish-item-group">
				<view class="close-btn">
					<uni-icons type="closeempty" size="30" @click="back"></uni-icons>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="写个标题" v-model="house.title" maxlength="20"/>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<textarea class="item-input" placeholder="详细介绍一下" v-model="house.introduce" maxlength="120"/>
					</view>
				</view>
				<view class="room-title">
					<view class="room-title-text">房源信息</view>
					<picker :range="houseRange" @change="houseChange" style="color: #666666; font-size: 16px;">
						<label>{{houseDefault}}</label>
					</picker>
				</view>
				<view class="room-info">
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.room" maxlength="2"/>
						<view class="item-text">房</view>
					</view>
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.parlor" maxlength="2"/>
						<view class="item-text">厅</view>
					</view>
					<view class="room-item">
						<input class="item-input" type="number" v-model="house.lavatory" maxlength="2"/>
						<view class="item-text">卫</view>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<picker @change="districtPickerChange" :value="districtIndex" :range="districtArray" range-key="name" class="item-input">
							<view>{{ districtArray[districtIndex].name }}</view>
						</picker>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="请填写房源详细地址" v-model="house.address" maxlength="25"/>
					</view>
				</view>
				<view class="item-group">
					<view class="item-input-group">
						<input class="item-input" placeholder="请填写每月租金或售价" v-model="house.price" @input="checkNum" maxlength="10"/>
					</view>
				</view>
				<view class="item-group" style="border-bottom: none;">
					<view class="item-bar">上传图片</view>
					<uni-file-picker :value="filePathsList" :auto-upload="false" file-mediatype="image" mode="grid" class="imgs"
								file-extname="png,jpg" :limit="9" @select="handleSelect" @delete="handleDelete" @success="success" />
				</view>
			</view>
		</view>
		<view class="btn-group">
			<view class="confirmBtn button" @click="publish">发布</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				filePathsList: [],
				house: {
					title: "",
					introduce: "",
					address: "",
					room: 1,
					parlor: 0,
					lavatory: 0,
					district: "城厢区",
					type: 0,
					price: "",
					pic: ""
				},
				houseRange: ["出租", "出售"],
				houseDefault: "出租",
				imgList: [],
				imgUrlList: [],
				districtArray: [{ name: '城厢区' }, { name: '涵江区' }, { name: '荔城区' }, { name: '秀屿区' }, { name: '仙游县' }],
				districtIndex: 0,
			};
		},
		methods: {
			handleSelect(res) {
				this.imgList.push(res.tempFilePaths[0]);
			},
			back() {
				uni.navigateBack()
			},
			houseChange(e) {
				console.log(e);
				let index = e.target.value;
				this.houseDefault = this.houseRange[index];
				this.house.type = index
			},
		  publish() {
				if (!this.checkHouseInfo()) {
					return;
				}
				this.$request.uploadCurrentImage(this.imgList).then(res=>{
					this.house.pic = JSON.stringify(res);
					this.$request.request({
						url: "/house/publish",
						method: "POST",
						data: this.house,
					}).then(res=>{
						console.log(res);
						if (res.code == 401) {
							uni.showModal({
								title: res.message + "，是否跳转登录界面",
								cancelText:'否',
								confirmText:'是',
								success(res){
								  if(res.cancel){
								    
								  }else if(res.confirm){
								    uni.navigateTo({
								    	url: "../user/login"
								    })
									}
								}
							})
						} else if (res.code == 200) {
							uni.showToast({
								title: "发布成功，跳转中...",
								icon: "none",
								duration: 2000
							});
							setTimeout(function() {
							  uni.navigateBack()
							}, 1500);
						}
					});
				})
				
			},
			checkHouseInfo() {
				// 标题
				if (this.house.title == "") {
					uni.showToast({
						title: "请填写标题",
						icon: "none"
					})
					return false;
				}
				if (this.house.title.length > 20) {
					uni.showToast({
						title: "标题过长",
						icon: "none"
					})
					return false;
				}
				
				// 描述
				if (this.house.introduce == "") {
					uni.showToast({
						title: "请填写描述",
						icon: "none"
					})
					return false;
				}
				if (this.house.introduce.length > 120) {
					uni.showToast({
						title: "描述过长",
						icon: "none"
					})
					return false;
				}
				
				// 房间数
				if (this.house.room.length <= 0) {
					uni.showToast({
						title: "请填写房间数",
						icon: "none"
					})
					return false;
				}
				if (this.house.room <= 0 || this.house.room > 99) {
					uni.showToast({
						title: "请正确填写房间数",
						icon: "none"
					})
					return false;
				}
				
				// 客厅数
				if (this.house.parlor.length <= 0) {
					uni.showToast({
						title: "请填写客厅数",
						icon: "none"
					})
					return false;
				}
				if (this.house.parlor < 0 || this.house.parlor > 99) {
					uni.showToast({
						title: "请正确填写客厅数",
						icon: "none"
					})
					return false;
				}
				
				// 厕所数
				if (this.house.lavatory.length <= 0) {
					uni.showToast({
						title: "请填写厕所数",
						icon: "none"
					})
					return false;
				}
				if (this.house.lavatory < 0 || this.house.lavatory > 99) {
					uni.showToast({
						title: "请正确填写厕所数",
						icon: "none"
					})
					return false;
				}
				
				// 地址
				if (this.house.address == "") {
					uni.showToast({
						title: "请填写房源详细地址",
						icon: "none"
					})
					return false;
				}
				if (this.house.address.length > 25) {
					uni.showToast({
						title: "详细地址过长",
						icon: "none"
					})
					return false;
				}
				
				// 区
				if (this.house.district == "") {
					uni.showToast({
						title: "请填写房源详细地址",
						icon: "none"
					})
					return false;
				} else {
					if (this.house.district == '城厢区' || this.house.district == '涵江区' || this.house.district == '荔城区'
						|| this.house.district == '秀屿区' || this.house.district == '仙游县') {
								
					}
					else {
						console.log(this.house.district);
						uni.showToast({
							title: "请正确填写莆田市内区划",
							icon: "none"
						})
						return false;
					}
				}
				
				// 价格
				if (this.house.price < 0 || this.house.price > 100000000) {
					uni.showToast({
						title: "请正确填写价格",
						icon: "none"
					})
					return false;
				}
				
				return true;
			},
			// 将当前选择的所有图片上传到后台
			uploadCurrentImage() {
				for (let i=0; i<this.imgList.length; i++) {
					this.$request.fileUploadImage(this.imgList[i]).then(fileRes => {
						fileRes = JSON.parse(fileRes)
						this.imgUrlList.push(fileRes.data.urls[0].url);
					});
				}
				this.house.pic = JSON.stringify(this.imgUrlList);
			},
			checkNum(e) {
			  //正则表达试
			  e.target.value = (e.target.value.match(/^\d*(\.?\d{0,2})/g)[0]) || null
			  //重新赋值给input
			  this.$nextTick(() => {
					this.house.price= e.target.value
					})
			},
			handleDelete(err) { // 删除图片
			    const num = this.filePathsList.findIndex(v => v.url === err.tempFilePath);
			    this.filePathsList.splice(num, 1);
					this.imgList = [];
			},
			districtPickerChange(e) {
				this.districtIndex = e.detail.value;
				this.house.district = this.districtArray[this.districtIndex].name;
			},
		}
	}
</script>

<style lang="scss">
	.publish-house-page {
		
		.publish-item-group {
			background-color: #FFFFFF;
			margin: 10rpx 10rpx;
			padding: 30rpx 20rpx;
			border-radius: 20rpx;
			
			.item-group {
				border-bottom: #f2f1f6 1rpx solid;
				
				.item-input-group {
					padding: 20rpx 0;
					
					.item-input {
						width: 100%;
						font-size: 16px;
						color: #000000;
						text-align: justify;
					}
				}
				
				.item-bar {
					font-size: 18px;
					margin-top: 10rpx;
				}
				
				.imgs {
					margin-top: 10rpx;
				}
			}
			
			.room-title {
				width: 100%;
				display: inline-flex;
				font-size: 18px;
				padding: 10px 0;
				
				.room-title-text {
					flex: 1;
				}
			}
			
			.room-info {
				display: inline-flex;
				
				.room-item {
					flex: 1;
					display: inline-flex;
					
					.item-input {
						width: 50%;
						text-align: center;
					}
					
					.item-text {
						width: 50%;
						font-size: 16px;
						color: #000000;
						
					}
				}
				
			}
			
		}
		
	}

</style>
